/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */


package ru.teleology.cubefs.editor;

import java.awt.GridLayout;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import ru.teleology.cubefs.api.models.Page;
import ru.teleology.cubefs.editor.hex.HexEditorPanel;
import ru.teleology.cubefs.editor.model.TreePageMetadataHelper;

public class PageEditorPanel extends JPanel{

	private HexEditorPanel hexTable = new HexEditorPanel();
	
	private JTabbedPane tabs = new JTabbedPane();
	
	private MainPanel navigation;
	
	public PageEditorPanel(MainPanel navigation) {
		this.navigation = navigation;
		
		tabs.addTab("Raw Data", new JScrollPane(hexTable));
		tabs.addTab("Page Content", new JPanel());
		this.setLayout(new GridLayout());
		navigation.focusData();
		hexTable.setNavigationTree(navigation);
		
		this.add(tabs);
		
		tabs.setMnemonicAt(0, KeyEvent.VK_R);
		tabs.setMnemonicAt(1, KeyEvent.VK_C);
		
		tabs.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				if (tabs.getSelectedIndex() == 0) {
					hexTable.focusTable();
				}
			}			
		});
	}
	
	public void setPage(Page page, TreePageMetadataHelper metadata) {
		hexTable.getTable().getHexModel().setPage(page, metadata);
	}

	public void focus() {
		hexTable.focusTable();
	}

	public void focusHextTable() {
		tabs.setSelectedIndex(0);
		hexTable.focusTable();
	}

	public void focusPageContent() {
		tabs.setSelectedIndex(1);
	}

	public void selectKeyField(int index) {
		hexTable.selectKeyField(index);
	}
	
}
